<?php
class Mngticket extends Controller {
	function Mngticket()  {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Ticketmodel','',TRUE);
		$this->load->model('Translog');
		$this->load->database();	
                session_start();
	}
	var $limit =20;
	function index() {
		if(((isset($_SESSION['sys_admin'])) and isset($_SESSION['name']))||((isset($_SESSION['admin']))and isset($_SESSION['name']))||((isset($_SESSION['intern_audit']))and isset($_SESSION['name']))||((isset($_SESSION['second_consult']))and isset($_SESSION['name']))||((isset($_SESSION['dept_pic']))and isset($_SESSION['name']))) {
			$this->get_last_ten_berita();
		} else {
			redirect('main');
		}
	}
	function get_last_ten_berita($offset = 0) {
		$this->data['title'] ='List Ticket'; 
		//offset
		$uri_segment = 4;
		$offset = $this->uri->segment($uri_segment);
		$this->data['cek']= $this->Ticketmodel->get_last_ten_berita($offset);
		// load data table berita_utama
		$tickets = $this->Ticketmodel->get_last_ten_berita($offset)->result();
		$num_rows = $this->Ticketmodel->count_all_num_rows();
		
		if($num_rows > 0) { // jika query menghasilkan data
			// membuat pagination
			$config['base_url'] = site_url('createticket/mngticket/get_last_ten_berita');
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->limit;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$this->data['pagination'] = $this->pagination->create_links();
			
			//set template table untuk efek selang-sling tiap baris
			$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0" width="880">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('<h3>No</h3>','<h3>Ticket ID</h3>','<h3>Name<h3>','<h3>Open Datetime</h3>','<h3>Target Date</h3>','<h3>Close Date</h3>','<h3>Status</h3>','<h3>Severity Level</h3>','<h3>Product</h3>','<h3>Actions</h3>');
			
			//Penomeran baris data
			$this->data['i'] =0 + $offset;
			$i = 0 + $offset;
			foreach($tickets as $ticket) {
				
				
				//penyusunan data baris perbaris
				
				$this->table->add_row(++$i,$ticket->ticket_id,$ticket->name,$ticket->open_datetime,$ticket->target_date,$ticket->close_date,$ticket->status,$ticket->severity_level,$ticket->product,
				anchor('createticket/mngticket/update_process/'.$ticket->ticket_id,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',array('class'=>'update')).''.
				anchor('createticket/mngticket/download_ticket/'.$ticket->ticket_id,'download',array('class'=>'download')));
				
			}
			$this->data['table'] = $this->table->generate();
		}
		else {
			$this->data['message'] = 'tidak di temukan satupun data ticket utama';
		}
		$this->data['link'] = array('link_add'=>anchor('createticket/ticket/','Create Ticket',array('class' =>'add')));
		
		//load default view
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('ticket/mngticket');
		$this->load->view('layout/slice_bawah');		
	}
	function delete($id_ticket) {
		$this->Ticketmodel->delete($id_ticket);
		redirect('create_ticket/Mngticket');
	}

	function coments2($id_show) {
		$this->data['form_action'] = site_url('createticket/mngticket/update_process');
		$cm_update = $this->Ticketmodel->coments2($id_show);
		$this->data['ticked'] = $id_show;
		foreach($cm_update->result() as $cm) {
			$this->data['comment_id'] = $cm->comment_id;
			$this->data['ticked_id'] = $cm->ticked_id;
			$this->data['comments'] = $cm->comments;
			$this->data['datetime'] = $cm->datetime;
			$this->data['images1'] = $cm->images1;
			$this->data['images2'] = $cm->images2;
			$this->data['images3'] = $cm->images3;
			$this->data['images4'] = $cm->images4;
			$this->data['images5'] = $cm->images5;
		}
		
		$idcmnt = $this->Ticketmodel->show_id_comments();
		foreach($idcmnt->result() as $cmnt) {
			$this->data['idcmnt'] = $cmnt->comment_id + 1;
		}
		// tentukan id coment
		
		//load default view
		$this->load->vars($this->data);
		$this->load->view('ticket/coments2');
		
	}
	
	function update_process($id_update=0) {
			$this->data['titles'] = 'Update Ticket';
			$this->data['h2_title'] = '';
			$this->data['form_action'] = site_url('createticket/mngticket/update_process');
			$id = $this->input->post('ticked_id');
			$x = $this->Ticketmodel->ubah_date($id)->row();
			$this->data['change'] =isset( $x->ticked_id);
			$this->data['prod'] = $this->Ticketmodel->show_product();
			$this->data['ubah'] = $this->input->post('ticked_id');
			
			/* Coment2
			--------------------------------------------------------------------------*/
			$comment2 = $this->Ticketmodel->show_id_coments2()->row();
			$this->data['idcmnt2'] = $comment2->id + 1;
			
			
			$b2 =$this->input->post('ticket_id');
			if(isset($id_update) !="0") {
				$ac = $id_update;
				} else {
					$ac = $this->input->post('ticket_id');
					}
			$this->data['show_all'] = $this->Ticketmodel->show_idcmnt_by_ticket($ac);
			/* End Coment2
			--------------------------------------------------------------------------*/
	
			$show =$this->data['update'] = $this->Ticketmodel->update_manage($id_update);
		foreach($show->result() as $shows) {
			$this->data['ticket_ids'] = $shows->ticket_id;
			$this->data['titled'] = $shows->title;
			$this->data['references'] = $shows->reference_id;
			$this->data['vul_dets'] = $shows->vul_detail;
			$this->data['vul_sols'] = $shows->sol_detail;
			$this->data['impacts'] = $shows->impact_detail;
			$this->data['open_dates'] = $shows->open_datetime;
			$this->data['probs'] = $shows->prob_o_impact;
			$this->data['levels'] = $shows->level_o_impact;
			$this->data['statuss'] = $shows->status;
			$this->data['user_ids'] = $shows->user_id;
			$this->data['target_date']= $shows->target_date;
			$this->data['close_dates'] = $shows->close_date;
			$this->data['names'] = $shows->name;
			$this->data['comentss']= $shows->notes;
			$this->data['products'] = $shows->prod_id;
			$this->data['images1s'] = $shows->images1;
			$this->data['images2s'] = $shows->images2;
			$this->data['images3s'] = $shows->images3;
			$this->data['images4s'] = $shows->images4;
			$this->data['images5s'] = $shows->images5;
			$this->data['images6s'] = $shows->images6;
			$this->data['images7s'] = $shows->images7;
			$this->data['images8s'] = $shows->images8;
			$this->data['images9s'] = $shows->images9;
			$this->data['images10s'] = $shows->images10;
			
			
			if(($shows->prob_o_impact * $shows->level_o_impact) == 9){
				$this->data['severitys'] = 'Critical';
			} else if(($shows->prob_o_impact * $shows->level_o_impact) == 6) {
				$this->data['severitys'] = 'High';
			} else if(($shows->prob_o_impact * $shows->level_o_impact) == 4) {
				$this->data['severitys'] = 'Medium';
			} else if(($shows->prob_o_impact * $shows->level_o_impact) == 3) {
				$this->data['severitys'] = 'Medium';
			} else if(($shows->prob_o_impact * $shows->level_o_impact) == 2) {
				$this->data['severitys'] = 'Low';
			} else if(($shows->prob_o_impact * $shows->level_o_impact) == 1) {
				$this->data['severitys'] = 'Note';
			} else {
				$this->data['severitys'] = '';
			}
			
		}
	
	
			$this->data['ticket_id'] = $this->input->post('ticket_id');
			$this->data['title'] = $this->input->post('title');
			$this->data['reference'] = $this->input->post('reference_id');
			$this->data['vul_det'] = $this->input->post('vul_detail');
			$this->data['vul_sol'] = $this->input->post('sol_detail');
			$this->data['impact'] = $this->input->post('impact_detail');
			$this->data['open_date'] = $this->input->post('open_datetime');
			$this->data['prob'] = $this->input->post('prob_o_impact');
			$this->data['level'] = $this->input->post('level_o_impact');
			$this->data['status'] = $this->input->post('status');
			$this->data['user_id'] = $this->input->post('user_id');
			$this->data['name'] = $this->input->post('user_id2');
			$this->data['coments']= $this->input->post('notes');
			$this->data['product'] = $this->input->post('prod_id');
			$this->data['coment_id'] =$this->input->get('ticket');
			$this->data['close_date'] =$this->input->post('close_date');
			$this->data['images1']=$this->input->post('images1');
			$this->data['images2']=$this->input->post('images2');
			$this->data['images3']=$this->input->post('images3');
			$this->data['images4']=$this->input->post('images4');
			$this->data['images5']=$this->input->post('images5');
			$this->data['images6']=$this->input->post('images6');
			$this->data['images7']=$this->input->post('images7');
			$this->data['images8']=$this->input->post('images8');
			$this->data['images9']=$this->input->post('images9');
			$this->data['images10']=$this->input->post('images10');
			
			/*Show Comments 2
			---------------------------------------------------------------------------------------*/
			
			/* end
			---------------------------------------------------------------------------------------*/
			
			// menentukan nilai severity of level
			if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 9){
				$this->data['severity'] = 'Critical';
			} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 6) {
				$this->data['severity'] = 'High';
			} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 4) {
				$this->data['severity'] = 'Medium';
			} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 3) {
				$this->data['severity'] = 'Medium';
			} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 2) {
				$this->data['severity'] = 'Low';
			} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 1) {
				$this->data['severity'] = 'Note';
			} else {
				$this->data['severity'] = '';
			}
			
			if(isset($_POST['kirim'])) {
			
						
					$number_of_file_fields = 0;
					$number_of_uploaded_files = 0;
					$number_of_moved_files = 0;
					$uploaded_files = array();
					$upload_directory = './uploads/'; //set upload directory
					
					for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
						$number_of_file_fields++;
						if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
							$number_of_uploaded_files++;
							$uploaded_files[] = $_FILES['images']['name'][$i];
							if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
								$number_of_moved_files++;
								
							}
							
							
						}
					}
			
			//mengambil array image upload
					if($uploaded_files[] = $_FILES['images']['name'][0]) {
						$a =$uploaded_files[0];
					} else {
						$a ="";
					}
					if($uploaded_files[] = (isset($_FILES['images']['name'][1]))) {
						$b =$uploaded_files[1];
					}else {
						$b ="";
					}
					if($uploaded_files[] = (isset($_FILES['images']['name'][2]))) {
						$c =$uploaded_files[2];
					}else {
						$c="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][3]))) {
						$d =$uploaded_files[3];
					}else {
						$d="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][4]))) {
						$e =$uploaded_files[4];
					}else {
						$e="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][5]))) {
						$f =$uploaded_files[5];
					}else {
						$f="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][6]))) {
						$g =$uploaded_files[6];
					}else {
						$g="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][7]))) {
						$h =$uploaded_files[7];
					}else {
						$h="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][8]))) {
						$ix =$uploaded_files[8];
					}else {
						$ix="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][9]))) {
						$j =$uploaded_files[9];
					}else {
						$j="";
					}
					
					// get Array 
					$update_c = array('ticket_id' => $this->input->post('ticket_id'),
									'reference_id' =>$this->input->post('reference_id'),
									'title' =>$this->input->post('title'),
									'vul_detail' =>$this->input->post('vul_detail'),
									'sol_detail' =>$this->input->post('sol_detail'),
									'impact_detail' =>$this->input->post('impact_detail'),
									'user_id' =>$this->input->post('user_id'),
									'open_datetime' =>date('Y-m-d H:i:s',strtotime($this->input->post('open_datetime'))),
									'target_date' =>$this->input->post('target_date'),
									'close_date'=>$this->input->post('close_date'),
									'status' =>$this->input->post('status'),
									'level_o_impact' =>$this->input->post('level_o_impact'),
									'prob_o_impact' =>$this->input->post('prob_o_impact'),
									'severity_level' =>$this->input->post('severity_level'),
									'prod_id' =>$this->input->post('prod_id'),
									'images1' =>$this->input->post(''),
									'images2' =>$this->input->post(''),
									'images3' =>$this->input->post(''),
									'images4' =>$this->input->post(''),
									'images5' =>$this->input->post(''),
									'images6' =>$this->input->post(''),
									'images7' =>$this->input->post(''),
									'images8' =>$this->input->post(''),
									'images9' =>$this->input->post(''),
									'images10' =>$this->input->post(''));
					//update ticket
					$id2 = $this->input->post('ticket_id');
					$this->Ticketmodel->update_ticket_process($id2,$update_c);
					
					$this->data['message'] = 'Update Ticket Sucess .....';
					
					/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
					/*$insert_c = array('comment_id' => $this->input->post('comment_id'),
									'ticked_id' =>$this->input->post('ticked_id'),
									'comments' =>$this->input->post('comments'),
									'datetime' =>date('Y-m-d H:i',strtotime($this->input->post('datetime')))),
									'images1' =>$a,
									'images2' =>$b,
									'images3' =>$c,
									'images4' =>$d,
									'images5' =>$e);
					
					$this->Ticketmodel->inset_cmmnt3($insert_c);*/
					
					
					/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
			/* Insert Log
			------------------------------------------------------------------------------*/
			if(getenv("HTTP_CLIENT_IP")) { 
				$ipad = getenv("HTTP_CLIENT_IP"); 
			} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
				$ipad = getenv("HTTP_X_FORWARDED_FOR"); 
			} else { 
				$ipad = getenv("REMOTE_ADDR"); 
			}
				$ipad = substr($ipad, 0,14);
				$translog_id = $this->Translog->show_id_trans_log()->row();
		
				$this->xd = $translog_id->id + 1;
		
		
				$this->send_id =$_SESSION['name'];
				$this->tgl = date('Y-m-d H:i:s');
				
				$insert_log = array('id' => $this->xd,
							'user_id' =>$this->send_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('Edit Ticket'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
				//insert ke table trans log
				$this->Translog->insert_log($insert_log);
				/* end Trnslog
				--------------------------------------------------------------------------------*/

				/* Insert Comments2
				------------------------------------------------------------------------------*/
				$idcmnt =array('id' =>$this->input->post('id'),
								'ticket_id' =>$this->input->post('ticket_id'),
								'user_id' =>$this->input->post('user_id'),
								'notes' =>$this->input->post('notes'),
								'tanggal' =>date('Y-m-d H:i',strtotime($this->input->post('tanggal'))),
								'images1' =>$a,
								'images2' =>$b,
								'images3' =>$c,
								'images4' =>$d,
								'images5' =>$e);
				$this->Ticketmodel->addComent2($idcmnt);
				
				
				/* end Insert Comments
				-------------------------------------------------------------------------------*/	
				$this->session->set_flashdata('message', 'Update data dan insert coment sukses');
				redirect('createticket/mngticket');
					
					
				
					
			}
			
					if(isset($_POST['save'])) {
					$number_of_file_fields = 0;
					$number_of_uploaded_files = 0;
					$number_of_moved_files = 0;
					$uploaded_files = array();
					$upload_directory = './uploads/coments/'; //set upload directory
					
					for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
						$number_of_file_fields++;
						if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
							$number_of_uploaded_files++;
							$uploaded_files[] = $_FILES['images']['name'][$i];
							if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
								$number_of_moved_files++;
								
							}
							
							
						}
					}
					// mengambil data array
					if($uploaded_files[] = (isset($_FILES['images']['name']['0']))) {
						$img1 = $uploaded_files[0];
					} else {
						$img1 =$this->input->post('images1');
					}
					if($uploaded_files[] = (isset($_FILES['images']['name']['1']))) {
						$img2 = $uploaded_files[1];
					} else {
						$img2 = $this->input->post('images2');
					}
					if($uploaded_files[] = (isset($_FILES['images']['name']['2']))) {
						$img3 = $uploaded_files[2];
					} else {
						$img3 =$this->input->post('images3');
					}
					if($uploaded_files[] = (isset($_FILES['images']['name']['3']))) {
						$img4 = $uploaded_files[3];
					} else {
						$img4 =$this->input->post('images4');
					}
					if($uploaded_files[] = (isset($_FILES['images']['name']['4']))) {
						$img5 = $uploaded_files[4];
					} else {
						$img5=$this->input->post('images5');
					}
					
					
					$this->form_validation->set_rules('comment_id','Coment ID','required');
					$this->form_validation->set_rules('ticked_id','Ticked ID','required');
					$this->form_validation->set_rules('comments','comments','required');
					$this->form_validation->set_rules('datetime','datetime','required');
					if($this->form_validation->run() == FALSE) {
					
						$this->session->set_flashdata('message', 'Create Comments Not Empty... !');
						/*echo "<script>window.close();opener.location.reload();</script>";*/

					
					
					} else {
												$insert_coments = array('comment_id' => $this->input->post('comment_id'),
												'ticked_id' => $this->input->post('ticked_id'),
												'comments' => $this->input->post('comments'),
												'datetime' => $this->input->post('datetime'),
												'images1' =>$img1,
												'images2' =>$img2,
												'images3' =>$img3,
												'images4' =>$img4,
												'images5' =>$img5);
						//cek comment if not empty
						$a = $this->input->post('comment_id');
						$xid = $this->Ticketmodel->cek_update_comments($a)->row();
					
							if($xid->comment_id == $a) {
								$this->Ticketmodel->update_comments($a,$insert_coments);
								echo "<script>window.close();opener.location.reload();</script>";
								
							} else {
								$this->Ticketmodel->insert_update_comments($insert_coments);
								echo "<script>window.close();opener.location.reload();</script>";
								
							}
					}
					
				}
			
			//load default view
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('ticket/update_ticket');
			$this->load->view('layout/slice_bawah');
			
			
	}
	
	function update_comments($idcmnt) {
			$this->data['form_action'] = site_url('createticket/mngticket/update_comments2_alls');
			
			
			$x = $this->Ticketmodel->update_comment($idcmnt)->row();
			$this->data['idcmnt'] 		= $x->id;
			$this->data['ticket_id'] 	= $x->ticket_id;
			$this->data['user_id'] 		= $x->user_id;
			$this->data['name']     	= $x->name;
			$this->data['notes'] 		= $x->notes;
			$this->data['tanggal'] 		= $x->tanggal;
			$this->data['images1'] 		= $x->images1;
			$this->data['images2']		= $x->images2;
			$this->data['images3'] 		= $x->images3;
			$this->data['images4'] 		= $x->images4;
			$this->data['images5']		= $x->images5;
			
			//load default view
			$this->load->vars($this->data);
			$this->load->view('ticket/comment2_update');
			
	}
	
	function update_comments2_alls() {
			$a1 = $this->input->post('id_cmnt');
			$b1 = array(
							'notes' =>$this->input->post('notes'),
							'tanggal' =>$this->input->post('tanggal'));
			
			//Update Comments
			$this->Ticketmodel->update_comments2_all($a1,$b1);
			echo "<script>window.close();opener.location.reload();</script>";
			
			
			
	}
	
	function download_ticket($idticket) {
		$this->data['tnj'] = $this->Ticketmodel->download_ticket($idticket);
		$this->load->vars($this->data);
		$down = $this->load->view('report/downtic','',TRUE);
		
		$file_name = "cetak_pdf2";
		
		$this->load->library('html2pdf/html2pdf');
		$html2pdf = new Html2Pdf('P','A4','en',array(5,5,5,5));
		$this->html2pdf->WriteHTML($down);
		$this->html2pdf->Output($file_name.'.pdf','D');
		
		redirect('createticket/mngticket');
		
	}
	
}
?>